package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2020-12-15
 */
@RestController
@RequestMapping("/admin/edu/teacher")
@Api(tags = "讲师管理模块") //描述controller的注解
public class AdminTeacherController {

    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("list")
    public R listAll(){
        List<Teacher> list = teacherService.list();
        return R.ok().data("items",list).message("获取讲师列表成功");
    }

    @ApiOperation(value = "根据ID删除讲师",notes = "根据ID删除讲师")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "讲师id",required = true) @PathVariable String id){
        boolean b = teacherService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("分页讲师列表")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Integer pageNum,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Integer pageSize,
                      TeacherQuery teacherQuery){
//        Page<Teacher> page = new Page<>(pageNum,pageSize);
//        page = teacherService.page(page);
        Page<Teacher> page = teacherService.selectTeacherPageByCondition(pageNum,pageSize,teacherQuery);
        return R.ok().data("page",page);
    }

    @ApiOperation("新增讲师")
    @PostMapping("save")
    public R saveTeacher(@ApiParam(value = "讲师对象", required = true) @RequestBody Teacher teacher){
        boolean b = teacherService.save(teacher);
        if (b){
            return R.ok().message("添加成功");
        }else {
            return R.error().message("添加失败");
        }
    }

    @ApiOperation("根据id获取讲师信息")
    @GetMapping("get/{id}")
    public R getTeacherById(@ApiParam(value = "讲师ID", required = true) @PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        if (teacher != null){
            return R.ok().data("item",teacher);
        }else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("更新讲师")
    @PutMapping("update")
    public R updateTeacherById(@ApiParam(value = "讲师对象", required = true) @RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if (b){
            return R.ok().message("修改成功");
        }else {
            return R.error().message("修改失败");
        }
    }
}

